package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.MyLog;
import com.sky.constant.MessageConstant;
import com.sky.constant.RedisConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    @Override
    public void insertByDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        //将菜品数据赋值给 数据库的实体类
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dish.setStatus(0);
//        将数据添加到数据库
        dishMapper.insertBydish(dish);
        stringRedisTemplate.delete(RedisConstant.CACHE_DISH);
    }

    @Override
    @MyLog
    public PageResult selectByPage(DishPageQueryDTO dishPageQueryDTO) {
        //开启自动分页
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //查询数据库中的数据 根据名称 菜品分类 售价状态进行模糊查询


        Page<Dish> dishes=dishMapper.selectByPage(dishPageQueryDTO);



        List<Dish> result = dishes.getResult();
        long total = dishes.getTotal();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(result);
        return pageResult;
    }

    @Override
    @Transactional
    public void deleteByids(List<Integer> ids) {
        //删除多个来自数据库的id
        for (Integer id : ids) {
            //首先 如果是启售状态则不能删除
            Dish dish=dishMapper.selectByidStatus(id);
            if (dish.getStatus()==1){
                throw new DeletionNotAllowedException("该套餐正在售出 不可删除");
            }
        }

        for (Integer id : ids) {
            //该套餐是否能删除？是否被关联了？
            //查询该菜品是否关联了套餐
            List<Integer> setmealIds = setmealDishMapper.selectByids(id);
            if (setmealIds != null && setmealIds.size() > 0) {
                //当前菜品被套餐关联了，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

        for (Integer id : ids) {
            dishMapper.delectByid(id);
            dishFlavorMapper.deleteByFlavor(id);
        }

    }

    @Override
    public void putstatus(Integer status, Integer id) {
        //修改这id 的状态
        dishMapper.putstatus(status,id);
    }

    @Override
    @Transactional
    public DishVO selectById(Integer id) {
        //根据id 查询 信息 进行回显
        Dish dish=dishMapper.selectByid(id);
        //创建 这个对象 将数据返回给他
        DishVO dishVO = new DishVO();
        //赋值
        BeanUtils.copyProperties(dish,dishVO);
        //查询口味
        List<DishFlavor>dishFlavors=dishFlavorMapper.selectByid(id);

        dishVO.setFlavors(dishFlavors);


        return dishVO;
    }

    @Override
    @Transactional
    public void putByList(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //设置时间
        dish.setUpdateTime(LocalDateTime.now());

        dish.setUpdateUser(BaseContext.getCurrentId());

        //修改用户信息
        dishMapper.putByList(dish);

        //删除口味信息
        dishFlavorMapper.deleteById(dishDTO.getId());

        //添加新的口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
                dishFlavorMapper.insertflavor(flavors);
        }



    }

    @Override
    public List<Dish> selectByCategoryId(Long categoryId) {
        Dish build = Dish.builder().categoryId(categoryId).status(1).build();
        List<Dish> dish = dishMapper.selectByCategoryId(build);
        return dish;
    }
}




















